import { createObject } from "../ast";
import { parse, print, prettyPrint } from "recast";
let {
  arrayExpression,
  exportNamedDeclaration,
  variableDeclaration,
  variableDeclarator,
  identifier
} = require("recast").types.builders;
/**
 * 递归
 * @param tdata
 * @param resData
 */
export function getRouter(arr) {
  let arr1 = [];
  function recursionArr(tdata, resData) {
    if (Array.isArray(tdata) && tdata.length > 0) {
      tdata.forEach((v, i) => {
        var arr = [];
        resData[i] = createObject(v);
        let children = resData[i].properties.find(
          (ele) => ele.key.name === "children"
        );
        recursionArr(v.children, arr);
        children.value = arrayExpression(arr);
      });
    }
  }
  recursionArr(arr, arr1);
  return print(
    exportNamedDeclaration(
      variableDeclaration("const", [
        variableDeclarator(identifier("routerArr"), arrayExpression(arr1))
      ])
    )
  ).code;
}

